<?php
/**
 * 音乐接口控制器 继承A控制器，A控制器的控制接口秘钥登录
 */
namespace Api\Controller;
use Think\Controller;
class MusicApiController extends AController {
    public function _empty(){
        echo '接口访问地址错误';
    }
    //获取所有音乐数据的接口
    public function get_all_music(){
        $p = I('p');
        if(!$p){
            $p = 1;
        }
        $num  = I('num');
        if(!$num){
            $num = 20;
        }
        $res = M('music_data')->page($p.','.$num)->select();
        if($res){
            foreach ($res as $key=>$val){
                $data[$key]['id']=$val['id'];
                $data[$key]['url']=$val['url'];
                $data[$key]['name']=$val['name'];
                $data[$key]['size']=$val['size'];
                $data[$key]['add_time']=$val['add_time'];
                $cate = M('music_cate')->where('id='.$val['cate'])->find();
                $data[$key]['cate']=$cate['name'];
            }
            $result = array(
                'code'=>1,
                'msg'=>'success',
                'data'=>$data
            );
        }else{
            $result = array(
                'code'=>0,
                'msg'=>'没有数据'
            );
        }
        $this->ajaxReturn($result);
    }

    //获取音乐分类
    public function get_music_cate(){
        $id=I('id');
        if(!$id){
            $res = M('music_cate')->field('id,name')->select();
        }else{
            $res = M('music_cate')->where('id='.$id)->field('id,name')->find();
        }
        $this->ajaxReturn($res);
    }

    //根据分类获取所有音乐数据的接口
    public function get_cate_music(){
        $cate1=I('cate1');
        if($cate1){
            $w['cate1']=$cate1;
        }
        $cate = I('cate');
        
        if(!$cate){
            $arr = array(
                'code'=>0,
                'msg'=>'参数错误，缺少分类参数'
            );
            $this->ajaxReturn($arr);
        }
        $w['cate']=$cate;
        $p = I('p');
        if(!$p){
            $p = 1;
        }
        $num  = I('num');
        if(!$num){
            $num = 20;
        }
        $res = M('music_data')->where($w)->page($p.','.$num)->select();
        if($res){
            foreach ($res as $key=>$val){
                $data[$key]['id']=$val['id'];
                $data[$key]['url']=$val['url'];
                $data[$key]['name']=$val['name'];
                $data[$key]['size']=$val['size'];
                $data[$key]['add_time']=$val['add_time'];
                $cate = M('music_cate')->where('id='.$val['cate'])->find();
                $data[$key]['cate']=$cate['name'];
                $data[$key]['cate1']=$val['cate1'];
            }
            $result = array(
                'code'=>1,
                'msg'=>'success',
                'data'=>$data
            );
        }else{
            $result = array(
                'code'=>0,
                'msg'=>'没有数据'
            );
        }
        $this->ajaxReturn($result);
    }
    //根据用户id获取所有音乐数据的接口
    public function get_user_music(){
        $userid = I('userid');
        if(!$userid){
            $arr = array(
                'code'=>0,
                'msg'=>'参数错误，缺少用户id参数'
            );
            $this->ajaxReturn($arr);
        }
        $w['uid']=$userid;
        $p = I('p');
        if(!$p){
            $p = 1;
        }
        $num  = I('num');
        if(!$num){
            $num = 20;
        }
        $res = M('music_data')->where($w)->page($p.','.$num)->select();
        if($res){
            foreach ($res as $key=>$val){
                $data[$key]['id']=$val['id'];
                $data[$key]['url']=$val['url'];
                $data[$key]['name']=$val['name'];
                $data[$key]['size']=$val['size'];
                $data[$key]['add_time']=$val['add_time'];
                $cate = M('music_cate')->where('id='.$val['cate'])->find();
                $data[$key]['cate']=$cate['name'];
            }
            $result = array(
                'code'=>1,
                'msg'=>'success',
                'data'=>$data
            );
        }else{
            $result = array(
                'code'=>0,
                'msg'=>'没有数据'
            );
        }
        $this->ajaxReturn($result);
    }

    //给音乐打分，参数：音乐id,音乐时间,局部评价和总体评价
    public function rate_music(){
        $mid = I('mid');
        $time = I('time');
        $ratepart = I('ratepart');
        $rateall = I('rateall');

        $data = array(
            'mid'=>$mid,
            'time'=>$time,
            'ratepart'=>$ratepart,
            'rateall'=>$rateall
        );
        $res = M('music_rate')->add($data);
        if($res){
            $arr = array(
                'code'=>1,
                'result'=>array(
                    'msg'=>'success',
                    'info'=>$data
                )
            );
        }
        $this->ajaxReturn($arr);
    }

    //获取音乐评价数据
    public function get_music_rate(){

        $p = I('p');
        if(!$p){
            $p = 1;
        }
        $num  = I('num');
        if(!$num){
            $num = 20;
        }
        $res = M('music_rate')->order('id desc')->page($p.','.$num)->select();
        if($res){
            foreach ($res as $key=>$val){
                $data[$key]['id']=$val['id'];
                $data[$key]['mid']=$val['mid'];
                $data[$key]['time']=$val['time'];
                $data[$key]['ratepart']=$val['ratepart'];
                $data[$key]['rateall']=$val['rateall'];
            }
            $result = array(
                'code'=>1,
                'msg'=>'success',
                'data'=>$data
            );
        }else{
            $result = array(
                'code'=>0,
                'msg'=>'没有数据'
            );
        }
        $this->ajaxReturn($result);
    }
}